/*
 * Created on Feb 17, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.OpenPurchaseOrderForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.Logger;


/**
 * @author Ravikanth
**/
public class OpenPurchaseOrderSaveCommand extends ActionCommand
{
	public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)	throws ScalarException
	{
		HashMap errors			= ( HashMap ) request.getAttribute( ERROR_MESSAGES );
		HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
		String actionForward 	= "saveSuccess";
		String action   		= request.getParameter( REQUEST_ACTION );
		HttpSession mSession 	= request.getSession();
		OpenPurchaseOrderForm purchaseOrderForm = ( OpenPurchaseOrderForm ) form;
		User user = (User)mSession.getAttribute( SessionKeys.USER_KEY );

		try
		{
			if( action.equalsIgnoreCase( "save" ) )
			{
				// Check wether the PO Number already Exists
				if( purchaseOrderForm.getPurchaseOrder_id() == -1 && PurchaseOrderDAO.isPurchaseOrderNumberExists( purchaseOrderForm.getPoNumber() ) )
				{
					purchaseOrderForm.setPoNumber( PurchaseOrderDAO.loadAutomatedPurchaseOrderNo () );
					request.setAttribute ( "financeReasonCodes", PurchaseOrderDAO.loadFinanceHolds() );
					request.setAttribute ( "opsReasonCodes", PurchaseOrderDAO.loadOperationsHolds() );
					info.put( "Purchase Order Number already exists. New Purchase Order number has been generated ", purchaseOrderForm.getPoNumber() );
					actionForward = "openPurchaseOrder";
				}
				else
				{
					// Save the Purchase Order
					PurchaseOrderDAO.saveOpenPurchaseOrder( purchaseOrderForm, user.getUserId() );
				
					if( purchaseOrderForm.getPath() == null || "create".equalsIgnoreCase( purchaseOrderForm.getPath() ) )
					{
						//Loading the Vendors of Fab, Sort, Assembly and Test.
						request.setAttribute( "vendorsList", PurchaseOrderDAO.loadPOVendors() );
						//Loading the Purchase Order Types.
						request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );

						actionForward = "saveSuccess";	
					}
					else if( "searchPendingPurchaseOrder".equalsIgnoreCase( purchaseOrderForm.getPath() ) )
					{
						request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
						request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
						request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );
						request.setAttribute( "purchaseOrderStatusList", PurchaseOrderDAO.loadPurchaseOrderStatusTypes( true) );
						request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );
						request.setAttribute( "action", ACTION_PENDING_PURCHASE_ORDER_SEARCH );
						actionForward = "pendingOrder";
					}
					else
					{
						request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
						request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
						request.setAttribute( "purchaseOrderTypesList", PurchaseOrderDAO.loadPurchaseOrderTypes() );
						request.setAttribute( "purchaseOrderStatusList", PurchaseOrderDAO.loadPurchaseOrderStatusTypes( false) );
						request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );
						request.setAttribute( "action", ACTION_CHANGE_PURCHASE_ORDER_SEARCH );
						actionForward = "changeOrder";
					}
				
					Logger.info( INFO_OPEN_PURCHASE_ORDER_INFORMATION_SAVED_SUCCESSFULLY );
					info.put( purchaseOrderForm.getPoNumber(), INFO_OPEN_PURCHASE_ORDER_INFORMATION_SAVED_SUCCESSFULLY );
                    info.put( "View/Print ", "Click here to <a href='javascript:showPoForViewPrint("+purchaseOrderForm.getPurchaseOrder_id()+");' onmouseover=\"window.status='Click here to View/print purchase order';return true\" onmouseout=\"window.status='';return true\"> view/print </a>Purchase Order Details");
				}
			}
		}
		catch ( Exception e )
		{
			Logger.error( ERROR_SAVING_OPEN_PURCHASE_ORDER_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, "" );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
			actionForward = "saveSuccess";
		}
        
		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		
		return mapping.findForward( actionForward );
	}
}
